﻿/*
 * Ext Gantt 1.2
 * Copyright(c) 2009-2011 Mats Bryntse Consulting
 * mats@ext-scheduler.com
 * http://www.ext-scheduler.com/license.html
 *
 */

Ext.ns('Sch.gantt.plugins');

/**
 * @class Sch.gantt.LabelEditor
 * @extends Ext.Editor
 * Class allowing editing the left and right task labels inline
 * @constructor
 * @param {Object} config The object containing the configuration 
 */
Sch.gantt.LabelEditor = function(gantt, config) {
    this.gantt = gantt; 
    gantt.on('afterrender', this.onGanttRender, this);
    Ext.apply(this, config);
    Sch.gantt.LabelEditor.superclass.constructor.call(this, config);
};
 
Ext.extend(Sch.gantt.LabelEditor, Ext.Editor, {
    
    // Programmatically enter edit mode
    edit : function(record) {
        var wrap = this.gantt.view.getElementFromEventRecord(record).up(this.gantt.eventWrapSelector);
        this.startEdit(wrap.child(this.delegate));
        this.record = record;
    },
    
    // private, must be supplied
    delegate : '',
    
    // private, must be supplied
    dataIndex : '',

    shadow: false,
    completeOnEnter: true,
    cancelOnEsc: true,
    ignoreNoChange: true,
    
    onGanttRender : function(gantt) {
        if (!this.field.width) {
            this.autoSize = 'width';
        }

        this.on({
            beforestartedit : function(editor, el, value) {
                return gantt.fireEvent('labeledit_beforestartedit', gantt, this.record, value);
            },
            beforecomplete : function(editor, value, original) {
                return gantt.fireEvent('labeledit_beforecomplete', gantt, value, original, this.record);
            },
            complete : function(editor, value, original) {
                this.record.set(this.dataIndex, value);
                gantt.fireEvent('labeledit_complete', gantt, value, original, this.record);
            },
            scope : this
        });
        
        gantt.getView().mainBody.on('dblclick', function(e, t){
            this.record = gantt.resolveTask(t);
            this.startEdit(t);
            this.setValue(this.record.get(this.dataIndex));
        }, this, {
            delegate: this.delegate
        });
    }
}); 
